/**
 * author      : dormi330
 * date        : 2018/6/28
 * project     : mybatis
 * description : 订单controller
 */

package com.deep.trade.adaptor.provided.rest;

import com.deep.trade.application.OrderServiceFacade;
import com.deep.trade.common.HttpResonsePayload;
import com.deep.trade.infrastructure.utils.CommonUtils;
import com.deep.trade.order.*;
import com.deep.trade.order.query.OutDtoOrderDetail;
import com.deep.trade.order.query.OutDtoTradeOrder;
import com.deep.trade.order.query.OutDtoTradeOrderWithSku;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;


@Controller
@RequestMapping(value = "/api/v1/order")
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private OrderServiceFacade orderServiceFacade;

    /**
     * 创建订单
     */
    @ResponseBody
    @RequestMapping(value = {"", "/"}, method = RequestMethod.POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    public HttpResonsePayload placeOrder(
            @RequestBody InDtoPlaceOrder inDtoPlaceOrder,
            @RequestParam String token) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoPlaceOrder);
        inDtoPlaceOrder.setToken(token);
        orderServiceFacade.placeOrder(inDtoPlaceOrder);
        return HttpResonsePayload.happy(true);
    }

    /**
     * 取消订单
     */
    @ResponseBody
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public HttpResonsePayload cancelTradeOrder(
            @RequestBody(required = false) InDtoCancelOrder inDtoCancelOrder,
            @RequestParam String token) throws Exception {
        logger.info("{}, tenantCode={}, inDtoCancelOrder={}", CommonUtils.currentClassAndMethod(), inDtoCancelOrder);
        inDtoCancelOrder.setToken(token);

        orderServiceFacade.cancelTradeOrder(inDtoCancelOrder);
        return HttpResonsePayload.happy();
    }

    /**
     * 订单列表, 不包含 sku
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderListOfMember(
            @RequestParam String tenantCode,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam String token) throws Exception {
        logger.info("{}, tenantCode={}, token={}", CommonUtils.currentClassAndMethod(), tenantCode, token);

        PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfMember(tenantCode, token, pageNum, pageSize);
        return HttpResonsePayload.happy(pageInfo);
    }

    /**
     * 订单列表, 包含 sku
     */
    @ResponseBody
    @RequestMapping(value = "/list-with-sku", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderWithSkuListOfMember(
            @RequestParam String tenantCode,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize,
            @RequestParam String token) throws Exception {
        logger.info("{}, tenantCode={}, token={}", CommonUtils.currentClassAndMethod(), tenantCode, token);


        PageInfo<OutDtoTradeOrderWithSku> pageInfo = orderServiceFacade.tradeOrderWithSkuListOfMember(tenantCode, token, pageNum, pageSize);
        return HttpResonsePayload.happy(pageInfo);
    }

    /**
     * 订单详情
     */
    @ResponseBody
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderDetail(
            @RequestParam String tenantCode,
            @RequestParam("id") String orderSummaryId,
            @RequestParam String token) throws Exception {
        logger.info("{}, tenantCode={}, orderSummaryId={}", CommonUtils.currentClassAndMethod(), tenantCode, orderSummaryId);
        // TODO token
        OutDtoOrderDetail outDtoOrderDetail = orderServiceFacade.tradeOrderDetail(tenantCode, orderSummaryId);
        return HttpResonsePayload.happy(outDtoOrderDetail);
    }

    /**
     * 订单审核
     */
    @ResponseBody
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public HttpResonsePayload auditTradeOrder(
            @RequestBody InDtoAuditOrder inDtoAuditOrder,
            @RequestParam String token) throws Exception {
        logger.info("{}, inDtoAuditOrder={}", CommonUtils.currentClassAndMethod(), inDtoAuditOrder);
        inDtoAuditOrder.setToken(token);
        // TODO token
        orderServiceFacade.auditTradeOrder(inDtoAuditOrder);
        return HttpResonsePayload.happy();
    }

    /**
     * 订单支付
     */
    @ResponseBody
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public HttpResonsePayload payTradeOrder(
            @RequestBody InDtoPayTradeOrder inDtoPayTradeOrder,
            @RequestParam String token) throws Exception {
        logger.info("{}, inDtoPayTradeOrder={}", CommonUtils.currentClassAndMethod(), inDtoPayTradeOrder);
        inDtoPayTradeOrder.setToken(token);

        orderServiceFacade.payTradeOrder(inDtoPayTradeOrder);
        return HttpResonsePayload.happy();

    }

    /**
     * 计算 若干 sku+qty 所需要付的钱的额度
     */
    @ResponseBody
    @RequestMapping(value = "/calculate-payment", method = RequestMethod.POST)
    public HttpResonsePayload calculatePayment(
            @RequestBody InDtoPaymentCalculate inDtoPaymentCalculate,
            @RequestParam String token
    ) throws Exception {
        logger.info("{}, inDtoPaymentCalculate={}", CommonUtils.currentClassAndMethod(), inDtoPaymentCalculate);
        inDtoPaymentCalculate.setToken(token);

        BigDecimal total = orderServiceFacade.calculatePayment(inDtoPaymentCalculate);
        return HttpResonsePayload.happy(total);
    }

    /**
     * 订单列表, 管理端使用
     */
    @ResponseBody
    @RequestMapping(value = "/mgmt/list", method = RequestMethod.GET)
    public HttpResonsePayload tradeOrderListOfTenant(
            @RequestParam String tenantCode,
            @RequestParam String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "10") int pageSize) throws Exception {
        logger.info("{}, tenantCode={}, pageSize={}, pageIndex={}", CommonUtils.currentClassAndMethod(), tenantCode);
        // TODO token
        PageInfo<OutDtoTradeOrder> pageInfo = orderServiceFacade.tradeOrderListOfTenant(tenantCode, pageNum, pageSize);
        return HttpResonsePayload.happy(pageInfo);
    }
}
